//
//  BSBEssenceController.m
//  Baisebudejie
//
//  Created by apple on 16/3/14.
//  Copyright © 2016年 youminuo. All rights reserved.
//

#import "BSBEssenceController.h"
#import "BSBTableController.h"



@interface BSBEssenceController () <UIScrollViewDelegate>

//标题视图
@property (weak, nonatomic) UIScrollView *titleScroll;
//内容底部视图
@property (weak, nonatomic) UIScrollView *contentScroll;
//标题数组
@property (strong, nonatomic) NSArray *textArray;
//标题栏下面的线
@property (weak, nonatomic) UIView *lineView;
//内容视图数组
@property (strong, nonatomic) NSMutableArray *contArray;
//当前选中的按钮
@property (weak, nonatomic) UIButton *titleSeleBtn;
//标题按钮数组
@property (strong, nonatomic) NSMutableArray *titleBtnArray;

@end

@implementation BSBEssenceController



#pragma mark - 内容视图的懒加载
- (NSMutableArray *)contArray
{
    if (_contArray == nil)
    {
        _contArray = [NSMutableArray array];
    }
    return _contArray;
}

- (NSMutableArray *)titleBtnArray
{
    if (_titleBtnArray == nil)
    {
        _titleBtnArray = [NSMutableArray array];
    }
    return _titleBtnArray;
}

#pragma mark - 对titleScroll的懒加载
- (UIScrollView *)titleScroll
{
    if (_titleScroll == nil)
    {
        UIScrollView *titleScroll = [[UIScrollView alloc] init];
        titleScroll.frame = CGRectMake(0, 64, self.view.width, 30);
        [self.view addSubview:titleScroll];
        self.titleScroll = titleScroll;
        titleScroll.showsHorizontalScrollIndicator = NO;
        titleScroll.showsVerticalScrollIndicator = NO;
        titleScroll.backgroundColor = [UIColor whiteColor];
    }
    return _titleScroll;
}

#pragma mark - 对contentScroll的懒加载
- (UIScrollView *)contentScroll
{
    if (_contentScroll == nil)
    {
        UIScrollView *contentScroll = [[UIScrollView alloc] init];
        contentScroll.frame = CGRectMake(0, CGRectGetMaxY(self.titleScroll.frame), self.view.width, self.view.height - CGRectGetMaxY(self.titleScroll.frame));
        [self.view addSubview:contentScroll];
        self.contentScroll = contentScroll;
        contentScroll.pagingEnabled = YES;
        contentScroll.bounces = NO;
        contentScroll.delegate = self;
    }
    return _contentScroll;
}

#pragma mark - textArray 的加载
- (NSArray *)textArray
{
    if (_textArray == nil)
    {
        _textArray = @[@"推荐", @"视频", @"图片", @"段子", @"网红", @"排行", @"社会", @"美女", @"游戏"];
    }
    return _textArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    修改标题和导航条按钮
    UIImageView *titleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    [titleImage sizeToFit];
    self.navigationItem.titleView = titleImage;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithimage:@"MainTagSubIcon" hightImage:@"MainTagSubIconClick" target:self action:@selector(itemDidClick)];
    
//    修改背景颜色    
    self.view.backgroundColor = BSBBackgoundColor;
    
//    关闭自动缩进
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setUptitleScroll];
    [self setUpcontTable];
    
//    初始时选中第一个按钮
    self.titleSeleBtn = self.titleBtnArray[0];
    self.titleSeleBtn.selected = YES;
}

#pragma mark - 设置标栏
- (void)setUptitleScroll
{
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = self.view.width /6;
    CGFloat btnH = self.titleScroll.height;
    
//    创建标签上的按钮
    for (int i = 0; i < self.textArray.count; ++i)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
//        设置按钮文字
        NSString *str = self.textArray[i];
        [btn setTitle:str forState:UIControlStateNormal];
        btnX = btnW * i;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [self.titleScroll addSubview:btn];
        
//        设置按钮的显示方式
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleBtnArray addObject:btn];
    }
    UIView *lineView = [[UIView alloc] init];
    [self.titleScroll addSubview:lineView];
    lineView.frame = CGRectMake(15, self.titleScroll.height-2, btnW - 30, 2);
    self.lineView = lineView;
    lineView.backgroundColor = [UIColor redColor];
    
//    设置标签栏的滚动范围
    self.titleScroll.contentSize = CGSizeMake(btnW * self.textArray.count, 0);
}

#pragma mark - 标题按钮的点击方法
- (void)btnDidClick:(UIButton *)sender
{
    self.titleSeleBtn.selected = NO;
    self.lineView.x = sender.x + 15;
    sender.selected = YES;
    self.titleSeleBtn = sender;
  
    
    CGFloat offsetX = sender.center.x - self.titleScroll.width * 0.5;
    CGFloat maxOffsetX = self.titleScroll.contentSize.width - self.view.width;
    if (offsetX< 0)
    {
        offsetX = 0;
    }
    else if(offsetX > maxOffsetX)
    {
        offsetX = maxOffsetX;
    }
    [self.titleScroll setContentOffset:CGPointMake(offsetX, 0) animated:YES];
  
    CGFloat contOffsetX = sender.tag * self.view.width;
    [self.contentScroll setContentOffset:CGPointMake(contOffsetX, 0) animated:YES];

}

#pragma mark - 设置内容视图
- (void)setUpcontTable
{
    CGFloat contViewX = 0;
    CGFloat contViewY = 0;
    CGFloat contViewW = self.view.width;
    CGFloat contViewH = self.contentScroll.height;
    for (int i = 0; i < self.textArray.count; ++i)
    {
        contViewX = contViewW * i;
        BSBTableController *tableView =[[BSBTableController alloc] init];
        tableView.view.frame = CGRectMake(contViewX, contViewY, contViewW, contViewH);
        [self addChildViewController:tableView];
        [self.contentScroll addSubview:tableView.view];
        [self.contArray addObject:tableView];
        
    }
    self.contentScroll.contentSize = CGSizeMake(self.view.width * self.textArray.count, 0);
}

- (void)itemDidClick
{
    
}

#pragma mark - 内容视图的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    UIButton *btn = self.titleBtnArray[index];
    [self btnDidClick:btn];
}



@end
